package android.medicalcloud.com.yiapp.yilian.ui.view.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.medicalcloud.com.yiapp.R;
import android.medicalcloud.com.yiapp.base.cach.PreferceManager;
import android.medicalcloud.com.yiapp.base.utils.DateTools;
import android.medicalcloud.com.yiapp.base.utils.Logger;
import android.medicalcloud.com.yiapp.base.utils.StringTools;
import android.medicalcloud.com.yiapp.yilian.service.DownLoadService;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;


/**
 *
 */
public class SureDialog extends Dialog {
    Context context;


    public SureDialog(final Context context) {
        super(context, R.style.CommonDialog);
        this.context = context;
        getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION);
        setContentView(R.layout.dialog_version);
        findViewById(R.id.btUpdate).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (onSureClickLisener!=null){
                    onSureClickLisener.onSureClick();
                }
                dismiss();
            }
        });
        ((Button)findViewById(R.id.btLater)).setText("取消");
        ((Button)findViewById(R.id.btUpdate)).setText("确定");
        ((TextView)findViewById(R.id.titleTv)).setText("提示");
        findViewById(R.id.btLater).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                dismiss();
            }
        });
        setCanceledOnTouchOutside(false);

    }

    @Override
    public void show() {
        Logger.e("isParentAlive =4343 "+!isParentAlive());

        if (!isParentAlive()) {
            return;
        }

        super.show();
    }



    public void setRes(String updateMes,boolean isM) {
        TextView content = (TextView) findViewById(R.id.content);
         findViewById(R.id.btLater).setVisibility(isM?View.GONE:View.VISIBLE);
        content.setText(updateMes);

    }


    @Override
    public void dismiss() {
        if (!isParentAlive()) {
            return;
        }
        super.dismiss();
    }

    private boolean isParentAlive() {
        try {
            Activity ac = (Activity) context;
            if (ac.isFinishing()) {
                return false;
            }
            return true;
        } catch (Exception e) {
            return false;
        }
    }
    public interface OnSureClickLisener{
        void onSureClick();
    }
    OnSureClickLisener onSureClickLisener;


    public void setOnSureClickLisener(OnSureClickLisener onSureClickLisener) {
        this.onSureClickLisener = onSureClickLisener;
    }
}
